
const request = require('../../../../utils/request');

Page({
    data: {
        mobile: '',
        code: '',
        codeText: '60S后再次发送',
        canIclick: false,
        sessionId: '',
        newUserId: 0,
        OldUserId: 0,
    },
    onLoad: function(e) {
        console.log(e)
        this.setData({
            mobile: e.mobile,
            sessionId: e.sessionId,
            OldUserId: e.userId
        })
        this.timeOut()
        
    },
    onReady: function(){
        wx.setNavigationBarTitle({
            title: '验证中心'
        })
    },
    getCode(e) {
        this.setData({
            code: e.detail.value
        })
    },
    validate() {
        var that = this;
        if(that.data.OldUserId == -1){
            request.HttpRequst(true, "program/check", 3, that.data.sessionId, { "mobile": that.data.mobile,"code": that.data.code,"token": that.data.sessionId}, "POST",false, res => {
                console.log(res)
                if(res.code == 200){
                    wx.setStorageSync('sessionId',that.data.sessionId);
                    wx.setStorageSync('userId',res.data.userId);    //存储新用户uuId
                    wx.switchTab({
                        url: '../../my'
                    })
                }else{
                    wx.showModal({
                        title: '提示',
                        content: '验证失败，请输入正确的验证码',
                        showCancel: false
                    })
                }
            })
        }else{
            request.HttpRequst(true, "program/check", 3, that.data.sessionId, { "mobile": that.data.mobile,"code": that.data.code,"token": that.data.sessionId,'userId':that.data.OldUserId}, "POST",false, res => {
                console.log(res)
                if(res.code == 200){
                    wx.setStorageSync('sessionId',that.data.sessionId);
                    wx.setStorageSync('userId',res.data.userId);    //存储新用户uuId
                    wx.switchTab({
                        url: '../../my'
                    })
                }else{
                    wx.showModal({
                        title: '提示',
                        content: '验证失败，请输入正确的验证码',
                        showCancel: false
                    })
                }
            })
        }
    },
    sendCode() {
        console.log("daial2")
        if(this.data.canIclick){
            var that = this
            request.HttpRequst(true, "program/send", false, "", { "mobile": this.data.mobile }, "POST",false, res => {
                if(res.code == 200){
                    console.log("成功!");
                }else{
                    wx.showModal({
                        title: '提示',
                        content: '请检查手机号是否正确',
                        showCancel: false
                    })
                    that.timeOut()
                }
            })
        }
        
    },
    timeOut() {
        var time = 60;
        var that = this
        let setTime = setInterval(function() {
            time--;
            that.setData({
                codeText: time+'S后再次发送',
                canIclick: false
            })
            if(time == 0){
                clearInterval(setTime);
                that.setData({
                    codeText: '再次发送',
                    canIclick: true
                })
            }
        },1000)
        
    }
})